package com.oa.server.controller;


import com.oa.server.entity.Admin;
import com.oa.server.entity.Role;
import com.oa.server.result.ResponseBean;
import com.oa.server.service.IAdminService;
import com.oa.server.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mcy
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "获取所有管理员")
    @GetMapping("/")
    public List<Admin> getAllAdmins(String keyWord){
        return adminService.getAllAdmins(keyWord);
    }

    @ApiOperation(value = "更新管理员")
    @PutMapping("/")
    public ResponseBean updateAdmin(@RequestBody Admin admin){
        if(adminService.updateById(admin)){
            return ResponseBean.success("更新成功");
        }
        return ResponseBean.error("更新失败");
    }

    @ApiOperation(value = "删除操作员")
    @DeleteMapping("/")
    public ResponseBean deleteAdmin(@PathVariable Integer adminId){
        if(adminService.removeById(adminId)){
            return ResponseBean.success("删除成功");
        }
        return ResponseBean.error("删除失败");
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/roles")
    public List<Role> getAllRoles(){
        return roleService.list();
    }

    @ApiOperation(value = "更新操作员角色")
    @PutMapping("/role")
    public ResponseBean updateAdminRole(Integer adminId,Integer[] rids){
        return adminService.updateAdminRole(adminId,rids);
    }
}
